package com.jinlia.show.modules.base.system.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinlia.show.common.core.exception.MyRuntimeException;
import com.jinlia.show.modules.base.constant.LabelEntityEnum;
import com.jinlia.show.modules.base.model.query.LabelQuery;
import com.jinlia.show.modules.base.model.vo.LabelVO;
import com.jinlia.show.modules.base.system.entity.Label;
import com.jinlia.show.modules.base.system.mapper.LabelMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class LabelMapperService extends ServiceImpl<LabelMapper, Label> {

    public Label addLabel(Label label) {

        List<Label> list = this.lambdaQuery()
                .eq(Label::getLabel, label.getLabel())
                .list();
        if (CollectionUtils.isEmpty(list)) { //已经存在标签对象和类型
            this.save(label);
        } else {
            //校验重复标签
            long count = list.stream().filter(i -> i.getLabel().equals(label.getLabel())).count();
            if (count > 0) {
                throw new MyRuntimeException("标签已存在");
            }
            this.save(label);
            this.lambdaUpdate()
                    .eq(Label::getLabel, label.getLabel())

                    .update(new Label());
        }
        return label;
    }

    /**
     * 查询标签
     */
    public List<Label> listName(String labelEntity, String labelType) {

        List<Label> list = this.lambdaQuery()
                .eq(Label::getLabel, labelEntity)
                .list();
        return list;
    }

    /**
     * 查询标签类型
     */
    public List<Label> listType(String labelEntity) {
        List<Label> list = this.lambdaQuery()
                .eq(Label::getLabel, labelEntity)
                .list();
        return list;
    }

    public IPage<Label> getPage(LabelQuery labelQuery) {
        // 构建分页参数
        Page<Label> page = new Page<>(labelQuery.getCurrent(), labelQuery.getSize());
        //构造查询参数
        QueryWrapper<Label> labelQueryWrapper = new QueryWrapper<>();
        labelQueryWrapper.eq(Objects.nonNull(labelQuery.getLabelEntity()),"label_entity",labelQuery.getLabelEntity());
        labelQueryWrapper.eq(Objects.nonNull(labelQuery.getLabelType()),"label_type",labelQuery.getLabelType());
        Page<Label> labelPage = this.getBaseMapper().selectPage(page, labelQueryWrapper);
        return labelPage;
    }

    public Label updateLabel(Label label) {
//        this.updateById(label);
//        this.lambdaUpdate()
//                .eq(Label::getId, label.getLabelEntity())
//                .set(Label::getTypeRemark, label.getTypeRemark())
//                .update(new Label());
        return label;
    }

    public List<LabelVO> treeList() {
        List<Label> list = this.lambdaQuery()
                .eq(Label::getEntity, LabelEntityEnum.GOODS.getValue())
                .list();
        Map<Long, LabelVO> collect = list.stream().collect(Collectors.toMap(Label::getId, LabelVO::fromEntity));
        for (Label label : list) {
            //找father
            if (collect.containsKey(label.getParentId())) {
                //找到
                collect.get(label.getParentId()).getChildren().add(collect.get(label.getId()));
            }
        }
        return collect.values().stream().filter(i -> i.getParentId() == null).collect(Collectors.toList()) ;
    }
}
